package com.babyshop.dao;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;

import com.babyshop.domain.Configuration;
import com.googlecode.s2hibernate.struts2.plugin.annotations.SessionTarget;
import com.googlecode.s2hibernate.struts2.plugin.annotations.TransactionTarget;

public class ConfigurationDaoImpl implements ConfigurationDao {

	@SessionTarget
	Session session;

	@TransactionTarget
	Transaction transaction;

	@Override
	public void saveOfUpdate(Configuration configuration) {
		try {
			session.saveOrUpdate(configuration);
		} catch (Exception e) {
			transaction.rollback();
			e.printStackTrace();
		}

	}

	@Override
	public void delete(int id) {
		try {
			Configuration configuration = (Configuration) session.get(Configuration.class, id);
			session.delete(configuration);
		} catch (Exception e) {
			transaction.rollback();
			e.printStackTrace();
		}

	}

	@Override
	public Configuration get(int id) {
		Configuration configuration = null;
		try {
			configuration = (Configuration) session.get(Configuration.class, id);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return configuration;
	}

	@Override
	@SuppressWarnings("unchecked")
	public List<Configuration> getAll() {
		List<Configuration> listConfigurations = null;
		try {
			listConfigurations = session.createQuery("from Configuration").list();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return listConfigurations;
	}

}
